package com.dragon.loan.service;

import com.alibaba.fastjson.JSON;
import com.dragon.loan.repository.AcLoanApplyInfoExtMapper;
import com.marvels.common.utils.MarvelsLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class AcLoanApplyInfoExtServiceImpl implements AcLoanApplyInfoExtDragonService {

	@Autowired
	private AcLoanApplyInfoExtMapper acLoanApplyInfoExtMapper;
	
	@Override
	public Integer queryExtInfoCountByLoanId(String loanId) {
		return acLoanApplyInfoExtMapper.queryExtInfoCountByLoanId(loanId);
	}
	
	@Override
	public void insertExtInfo(Map<String, Object> columnValue) {
		acLoanApplyInfoExtMapper.insertExtInfo(columnValue);
	}


	@Override
	public void updateExtInfoByLoanId(Map<String, Object> columnValue, String loanId) {
		acLoanApplyInfoExtMapper.updateExtInfoByLoanId(columnValue, loanId);
	}

	@Override
	public boolean operateExtColumn(String loanId, Map<String, Object> columnValue) {
		boolean result = false;
		if(columnValue != null){
			Integer count = this.queryExtInfoCountByLoanId(loanId);
			if (count > 0) {
				// 存在记录就修改
				this.updateExtInfoByLoanId(columnValue, loanId);
			} else {
				// 不存在新增
				columnValue.put("loan_id", loanId);
				this.insertExtInfo(columnValue);
			}
			result = true;
		}
		return result;
	}

	@Override
	public Map<String, String> queryExtInfoByLoanId(String loanId) {
		return acLoanApplyInfoExtMapper.queryExtInfoByLoanId(loanId);
	}

	@Override
	public Map<String, String> getExtInfoByLoanId(String loanId) {
		return acLoanApplyInfoExtMapper.getExtInfoByLoanId(loanId);
	}

	@Override
	public boolean isReLoan(String loanId) {
		boolean isReLoan = false;
		// 扩展信息
		Map<String, String> extMap = this.getExtInfoByLoanId(loanId);
		MarvelsLogUtil.getInstance().info("======获取扩展信息:" + JSON.toJSONString(extMap));
		if (extMap != null && "1".equals(extMap.get("reloan_flag"))) {
			isReLoan = true;
		}
		return isReLoan;
	}

}
